<%@page language="java" pageEncoding="UTF-8"%>
<%@page import="com.qidian.common.beans.ClientBean"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
ClientBean client= (ClientBean)request.getAttribute("client") ;

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>答题客户端</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<script type="text/javascript" src="js/ext-all.js"></script>
  	<script type="text/javascript">
  	var getCountInterval ;

	// For IE, trim is unsupported.
	if(typeof String.prototype.trim !== 'function') {
	  String.prototype.trim = function() {
	    return this.replace(/^\s+|\s+$/g, ''); 
	  }
	}
  	
  	function getCount()
  	{
		var urlGetCount = "client?actionType=getCount&clientId=<%=client.getClientId()%>" ;
		Ext.Ajax.request({
		url:urlGetCount,
		method:'GET',
		success:function(transport){
			if (transport == null || transport.responseText == '')
			{
				clearInterval(getCountInterval);
				alert('请重新登陆') ;
			}		
			var data = eval('('+transport.responseText+')');
			if (data.answerCount == null)
			{
				clearInterval(getCountInterval);
				alert('请重新登陆') ;
			}
			else
			{
				document.getElementById('answerCount').innerHTML = data.answerCount ;
				document.getElementById('wrongCount').innerHTML = data.wrongCount ;
				document.getElementById('answerWaited').innerHTML = data.answerWaited ;
				document.getElementById('point').innerHTML = data.point ;
				if ((document.getElementById('msgCount').value == 0 ||
						document.getElementById('contentDiv').innerHTML.trim() == '')
						 && data.answerWaited != 0)
				{
					var guid = Math.floor(Math.random()*100000) ;
					var urlRequestMsg = "client?actionType=requestMsg&clientId=<%=client.getClientId()%>&rid="+guid ;
					Ext.Ajax.request({
					url:urlRequestMsg,
					async: false ,
					method:'GET',
					success:function(transport){
						var data = eval('('+transport.responseText+')');
						var innerHTML = '' ;
						for ( var i = 0 ; i < data.msg.length ; i++)
						{

							innerHTML += '<tr><td><img width=168 height=128 src=\''+
									'file?actionType=getFile&msgId='+data.msg[i].id+'\'/></td>'+
									'<td><input type=text name=\'msgAnswers\' id=\''+data.msg[i].id+'\' value=\'\' </td>'+
							'</tr>';
							if (i%2 == 1)
								innerHTML += '<br>';
						}
						if (data.msg.length>0)
							innerHTML += '<br><tr><td><input value="发送答案" type=submit onClick=\'sendAnswer();\' /></td></tr>'
						document.getElementById('contentDiv').innerHTML = innerHTML ;
						document.getElementById('msgCount').value = data.msg.length ;
						var inputs = document.getElementsByName('msgAnswers') ;
						if (inputs != null && inputs[0] != null)
							inputs[0].focus() ;
					},
					failure: function(){
						alert('error');
						clearInterval(getCountInterval);
						alert('请重新登陆') ;
					}
					}) ;
				}
			}
		},
		failure: function(){
			clearInterval(getCountInterval);
			alert('请重新登陆') ;
		}
		}) ;
  	}
	
	function sendAnswer()
	{
		var msgCount = document.getElementById('msgCount').value ;
		var surl = 'client?actionType=sendAnswers&clientId=<%=client.getClientId()%>&answers=' ;
		var answers = '' ;
		var msgId ;
		var value ;
		var answer = '';
		var elements = document.getElementsByName('msgAnswers');
		for (var i = 0 ; i < elements.length ; i ++)
		{
			if (i>0)
				answer += ';' ;
			value = elements[i].value.trim() ;
			if (value == '' || value.length > 8)
			{
				alert('非法答案') ;
				return ;
			}
			answer += elements[i].id + ':' + encodeURI(encodeURI(value)) ;
		}
		surl += answer ;
		Ext.Ajax.request({
			'url':surl,
			method:'GET',
			success:function(tranport){
				document.getElementById('contentDiv').innerHTML = '';
				document.getElementById('msgCount').value = 0 ;
			},
			failed:function(){
				clearInterval(getCountInterval);
				alert('发送答案失败，请重新登录') ;
			}
		}) ;
	}
	function logout()
	{
		var surl = 'login?actionType=logout&clientId=<%=client.getClientId()%>&userName=<%=client.getClientName()%>&password=<%=client.getPassword()%>' ;
		Ext.Ajax.request({
			'url':surl,
			method:'GET',
			success:function(tranport){
				document.getElementById('logout').value = '已注销';
				document.getElementById('logout').onClick = null ;
			},
			failed:function(){
				alert('注销失败，请重新登录') ;
			}
		}) ;	

	}
	function sendAnswerByKey(event)
	{
		if (document.getElementById('msgCount').value == 0)
			return ;
		var evt=event?event:(window.event?window.event:null);
		if (evt.keyCode == 13)
			sendAnswer() ;
	}
	if (<%= client!= null && client.isActive()%>)
		getCountInterval = setInterval(getCount,1000); 
  	</script>
  </head>

  <body onkeyup="sendAnswerByKey(event);">
  	<div name="countDiv" id="countDiv">
  		<tr>
  			<td><span>客户名:</span></td><td><span id="userName"><%=client==null?"-":client.getClientName() %></span></td>
  			<td><span>总数:</span></td><td><span id="answerCount"><%=client==null?"-":client.getAnswered() %></span></td>
			<td><span>错题:</span></td><td><span id='wrongCount'><%=client==null?"-":client.getWrong() %></span></td>
  			<td><span>待答:</span></td><td><span id='answerWaited'><%=client==null?"-":client.getMsgCount() %></span></td>
  			<td><span>点数:</span></td><td><span id='point'><%=client==null?"-":client.getPoint() %></span></td>
			<td><input type="hidden" name="msgCount" id="msgCount" value="0" />
			<td><input type="button" name="logout" id="logout" value="注销" onClick="logout();"/>
		</tr>
  	</div>
	<div name="contentDiv" id="contentDiv"/>
  </body>
</html>
